package com.dev.sacot41.scviewpager;

import ohos.agp.components.Component;

/**
 * Created by Samuel on 2015-07-06.
 */
public class SCPositionAnimation extends SCPageAnimation {

    /**
     * 第page页需要移动的x值
     */
    public int xPosition;

    /**
     * 第page页需要移动的y值
     */
    public int yPosition;

    private float xStartPosition;
    private float yStartPosition;

    /**
     * @param forPage page to apply animation
     * @param dx      x moving, in dp
     * @param dy      y moving, in dp
     */
    public SCPositionAnimation(int forPage, int dx, int dy) {
        this.page = forPage;
        this.xPosition = dx;
        this.yPosition = dy;
        this.xStartPosition = 0;
        this.yStartPosition = 0;
    }

    /**
     * 设置具体控件最开始状态的位置偏移量
     *
     * @param startX x偏移量
     * @param startY y偏移量
     */
    protected void setStart(Integer startX, Integer startY) {
        if (startX != null) {
            this.xStartPosition = startX;
        }
        if (startY != null) {
            this.yStartPosition = startY;
        }
    }

    @Override
    public void applyTransformation(Component onView, float positionOffset) {
        onView.setTranslationX((int) (xPosition * positionOffset) + xStartPosition);
        onView.setTranslationY((int) (yPosition * positionOffset) + yStartPosition);
        onView.postLayout();
    }

}
